package com.turing.usdtpay.mvp.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.turing.usdtpay.R;
import com.turing.usdtpay.base.BaseActivity;
import com.turing.usdtpay.entity.NoticeInfoData;
import com.turing.usdtpay.mvp.contract.NoticeContract;
import com.turing.usdtpay.mvp.presenter.NoticePresenter;
import com.turing.usdtpay.mvp.view.BaseTitleView;
import com.turing.usdtpay.mvp.view.webwrapper.SDKWebView;
import com.turing.usdtpay.mvp.view.webwrapper.SDKWebViewClient;
import com.turing.usdtpay.utils.LogUtils;
import com.turing.usdtpay.utils.ToastUtils;

/**
 * @作者：JAY
 * @创建时间：2019/10/21 18:07
 * @描述：
 **/
public class NoticeActivity extends Activity implements NoticeContract.View {

    private BaseTitleView mTitleView;
    private SDKWebView mWebView;
    private String mUrl = "";//隐私政策的html文件位置
    private NoticeContract.Present mPresent;
    private String mTitle;
    private int mId;
    private SDKWebViewClient mWebViewClient = new SDKWebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
//            if (mActivity != null)//防止中途退出弹框获取不到Activity
//                hideLoading();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
//            if (mActivity != null)//防止中途退出弹框获取不到Activity
//                showLoading();
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            LogUtils.d("网页加载：" + url);
//            try {
//                if (url.startsWith("weixin://")) {
//                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//                    mActivity.startActivityForResult(intent, 10086);
//                    return true;
//                } else if (!(url.startsWith("http://") || url.startsWith("https://"))) {
//                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//                    mActivity.startActivityForResult(intent, 10086);
//                    return true;
//                }
//            } catch (Exception e) {
//                return false;
//            }

//            LogUtils.d("结果--》"+"/s-"+mSid+"/n-"+mRoleName+"/");
//            if (url.contains("/pay/") && url.contains("/s-"+mSid+"/n-"+mRoleName+"/")) {
//                dismiss();
//                return true;
//            }
//            Map<String, String> header = new HashMap<>();
//            header.put("Referer", mReferer);
            mWebView.loadUrl(url);
//            mReferer = url;
            return true;
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agreement);
        initView();
        initData();
        initTitle();
    }



    private void initData() {
        setPresenter(new NoticePresenter(this));
        Intent intent = getIntent();
        if (intent != null){
            mTitle = intent.getStringExtra("title");
            mId = intent.getIntExtra("id",0);
            mPresent.getNotice(this,mId);
        }
    }

    private void initTitle() {
        mTitleView.setLeftShow(true);
        mTitleView.setLogoShow(false);
        mTitleView.setTitle(mTitle);
        mTitleView.setLeftButtonListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {
        mTitleView = (BaseTitleView) findViewById(R.id.title);
        mWebView = (SDKWebView) findViewById(R.id.webView);
        settingWebView();
    }

    @SuppressLint("AddJavascriptInterface")
    private void settingWebView() {
        // 设置属性
        WebSettings settings = mWebView.getSettings();
        settings.setDomStorageEnabled(true);////启用或禁用DOM缓存。
        mWebView.setWebViewClient(mWebViewClient);
        settings.setUseWideViewPort(true);//设置webview推荐使用的窗口
        settings.setLoadWithOverviewMode(true);//设置webview加载的页面的模式
        settings.setSupportZoom(true); // 支持缩放

//        settings.setBuiltInZoomControls(true);
//        settings.setDisplayZoomControls(false);
//        // 设置弹窗的监控和js接口
//        mWebView.setEnableHardwareAccelerated(false);


        settings.setAppCacheMaxSize(1024 * 1024 * 8);
        String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
        settings.setAppCachePath(appCachePath);
        settings.setAllowFileAccess(true);
        settings.setAppCacheEnabled(true);
        settings.setTextZoom(300);
        /**
         * 用WebView显示图片，可使用这个参数 设置网页布局类型：
         * 1、LayoutAlgorithm.NARROW_COLUMNS ：适应内容大小
         * 2、LayoutAlgorithm.SINGLE_COLUMN:适应屏幕，内容将自动缩放
         * 3.NORMAL：正常显示不做任何渲染
         */
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);


        //透明
        mWebView.setBackgroundColor(getResources().getColor(R.color.colorWhite));
    }

    @Override
    public void onSuccess(NoticeInfoData data) {
        LogUtils.d("onSuccess data :"+data.toString());
        mUrl = data.getContent();
        // 设置WevView要显示的网页
        mWebView.loadDataWithBaseURL(null, mUrl, "text/html", "utf-8",
                null);
    }

    @Override
    public void onFail(String msg) {
        ToastUtils.showNormolToast(this,msg);
    }

    @Override
    public void setPresenter(NoticeContract.Present presenter) {
        mPresent = presenter;
    }
}
